#! coding=utf-8
from flask import render_template, redirect, Response, jsonify, url_for
from sqlalchemy.orm import sessionmaker
from app import app
import json
from models import Topic, User, Comment
from sqlalchemy import desc


@app.route('/')
def index_page():
    return redirect(url_for('index', page=1))


@app.route('/<int:page>')
def index(page=1):
    pagination = Topic.query.order_by(desc(Topic.ctime)).paginate(page, per_page=10,
                                                                  error_out=False)
    all_list = pagination.items
    l_all = []
    ptop = 0
    for i in all_list:
        q_user = User.query.get(i.uid)
        if i.type == 10:
            i.image = 'http://wimg.spriteapp.cn/%s' % i.image
        if i.type == 41:
            i.bimageuri = 'http://wimg.spriteapp.cn%s' % i.bimageuri
            i.videouri = 'http://wvideo.spriteapp.cn%s' % i.videouri
            ptop = size_process(i.size)
        dic = dict(id=i.id, title=i.body, love=i.love, hate=i.hate, ctime=str(i.ctime), video_img=i.bimageuri,
                   video_uri=i.videouri, type=i.type, name=q_user.name, image=i.image, forward=i.forward,
                   header=header_process(q_user.header), padtop=ptop)
        l_all.append(dic)
    page_dic = {'pagination': pagination, 'type': 0}
    return render_template('all.html', dst=l_all, pageinfo=page_dic)


@app.route('/video/<int:page>')
def video(page=1):
    pagination = Topic.query.filter_by(type=41).order_by(desc(Topic.ctime)).paginate(page, per_page=10,
                                                                                     error_out=False)
    all_list = pagination.items
    l_all = []
    for i in all_list:
        q_user = User.query.get(i.uid)
        dic = dict(id=i.id, title=i.body, love=i.love, hate=i.hate, ctime=str(i.ctime),
                   video_img='http://wimg.spriteapp.cn%s' % i.bimageuri,
                   video_uri='http://wvideo.spriteapp.cn%s' % i.videouri,
                   name=q_user.name, header=header_process(q_user.header), forward=i.forward,
                   padtop=size_process(i.size))
        l_all.append(dic)
    page_dic = {'pagination': pagination, 'type': 41}
    return render_template('video.html', dst=l_all, pageinfo=page_dic)


@app.route('/<int:pid>.html')
def detail(pid=26680902):
    qset = Topic.query.get(int(pid))
    quser = User.query.get(qset.uid)
    qcmt = Comment.query.filter_by(pid=pid, cmt_type=29)[:10]
    ptop = 0
    if qset is None:
        return Response('Sorry! Not Found!')
    if qset.type == 10:
        qset.image = 'http://wimg.spriteapp.cn/%s' % qset.image
    if qset.type == 41:
        qset.bimageuri = 'http://wimg.spriteapp.cn%s' % qset.bimageuri
        qset.videouri = 'http://wvideo.spriteapp.cn%s' % qset.videouri
        ptop = size_process(qset.size)
    l_all = []
    dic = dict(id=qset.id, title=qset.body, love=qset.love, hate=qset.hate, ctime=str(qset.ctime),
               video_img=qset.bimageuri, video_uri=qset.videouri, type=qset.type, name=quser.name, image=qset.image,
               forward=qset.forward, header=header_process(quser.header))
    l_all.append(dic)

    return render_template('detail.html', dst=l_all, padtop=ptop, title_type=True, cmt=qcmt)


@app.route('/pic/<int:page>')
def pic(page=1):
    pagination = Topic.query.filter_by(type=10).order_by(desc(Topic.ctime)).paginate(page, per_page=10, error_out=False)
    all_list = pagination.items
    l_all = []
    for i in all_list:
        q_user = User.query.get(i.uid)
        dic = dict(id=i.id, title=i.body, love=i.love, hate=i.hate, ctime=str(i.ctime), forward=i.forward,
                   name=q_user.name, image='http://wimg.spriteapp.cn/%s' % i.image,
                   header=header_process(q_user.header))
        l_all.append(dic)
    page_dic = {'pagination': pagination, 'type': 10}
    return render_template('pic.html', dst=l_all, pageinfo=page_dic)


@app.route('/text/<int:page>')
def text(page=1):
    pagination = Topic.query.filter_by(type=29).order_by(desc(Topic.ctime)).paginate(page, per_page=10,
                                                                                     error_out=False)
    all_list = pagination.items
    l_all = []
    for i in all_list:
        q_user = User.query.get(i.uid)
        dic = dict(id=i.id, title=i.body, love=i.love, hate=i.hate, ctime=str(i.ctime),
                   name=q_user.name, header=header_process(q_user.header), forward=i.forward)
        l_all.append(dic)
    page_dic = {'pagination': pagination, 'type': 29}
    return render_template('text.html', dst=l_all, pageinfo=page_dic)


def page_process(pagination):
    page_list = []
    for p in pagination.iter_pages(left_edge=0, left_current=2, right_current=3, right_edge=0):
        if p:
            page_list.append(p)
    page_info = {'pre': pagination.prev_num, 'next': pagination.next_num, 'cur': pagination.page, 'range': page_list}
    return page_info


def header_process(header):
    if 'http' not in header:
        return 'http://wimg.spriteapp.cn/profile/%s' % header
    return header


def size_process(size):
    if size is not None:
        lsize = str(size).split(':')
        ptop = float(lsize[1])/float(lsize[0])*100
        if ptop > 100:
            return 100
        return ptop
    return 56.66

